package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.req.IdParam;
import com.example.common.resp.Result;
import com.example.springboot.dto.RoutesResp;
import com.example.springboot.entity.MenuEntity;
import com.example.springboot.entity.RoutesEntity;
import com.example.springboot.entity.SubmenuEntity;
import com.example.springboot.service.MenuService;
import com.example.springboot.service.RoutesService;
import com.example.springboot.service.SubmenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 路由关联表 前端控制器
 * </p>
 *
 * @author he
 * @since 2023-11-16
 */
@Tag(name = "父子路由关联")
@RestController
@RequestMapping("/routesEntity")
public class RoutesController {
    @Autowired
    private RoutesService routesService;
    @Autowired
    private SubmenuService submenuService;
    @Autowired
    private MenuService menuService;

    @Operation(summary = "是否存在")
    @PostMapping("/exist")
    public Integer exist(@RequestBody @Validated IdParam param) {
        QueryWrapper<RoutesEntity> routesEntityQueryWrapper = new QueryWrapper<>();
        routesEntityQueryWrapper.eq("id", param.getId());
        if (routesService.count(routesEntityQueryWrapper) == 0) {
            return 0;
        }
        return 1;
    }

    @Operation(summary = "关联列表")
    @GetMapping("/list")
    public Result<List<RoutesResp>> list() {
        List<RoutesEntity> routesEntityList = routesService.list();
        ArrayList<RoutesResp> routesRespArrayList = new ArrayList<>();
        for (int i = 0; i < routesEntityList.size(); i++) {
            RoutesEntity routesEntity = routesEntityList.get(i);
            RoutesResp routesResp = new RoutesResp();
            BeanUtils.copyProperties(routesEntity, routesResp);
            //向下搜索
            routesResp.setSubmenuEntity(submenuService.getById(routesEntity.getSubmenuId()));
            routesResp.setMenuEntity(menuService.getById(routesEntity.getMenuId()));
            //添加
            if (routesEntity.getSubmenuId() != 0) {
                routesRespArrayList.add(routesResp);
            }
        }
        return Result.success(routesRespArrayList);
    }

    @Operation(summary = "根据id查找")
    @PostMapping("/detail")
    public Result<RoutesResp> detail(@RequestBody @Validated IdParam param) {
        //关联是否存在
        if (exist(param) == 0) {
            return Result.failure("关联不存在");
        }
        RoutesResp routesResp = new RoutesResp();
        RoutesEntity routesEntity = routesService.getById(param.getId());
        BeanUtils.copyProperties(routesEntity, routesResp);
        //一级路由详情
        routesResp.setSubmenuEntity(submenuService.getById(routesEntity.getSubmenuId()));
        //二级路由详情
        routesResp.setMenuEntity(menuService.getById(routesEntity.getMenuId()));
        if (routesResp.getSubmenuId() == 0) {
            return Result.failure("关联不存在");
        }
        return Result.success(routesResp);
    }

    @Operation(summary = "保存或修改")
    @PostMapping("/save")
    public Result save(@RequestBody @Validated RoutesEntity param) {
        RoutesEntity routesEntity = new RoutesEntity();
        BeanUtils.copyProperties(param, routesEntity);
        //一级路由是否存在
        QueryWrapper<SubmenuEntity> submenuEntityQueryWrapper = new QueryWrapper<>();
        submenuEntityQueryWrapper.eq("id", param.getSubmenuId());
        if (submenuService.count(submenuEntityQueryWrapper) == 0) {
            return Result.failure("一级路由不存在ID");
        }
        //二级路由是否存在
        QueryWrapper<MenuEntity> menuEntityQueryWrapper = new QueryWrapper<>();
        menuEntityQueryWrapper.eq("id", param.getMenuId());
        if (menuService.count(menuEntityQueryWrapper) == 0) {
            return Result.failure("二级路由不存在ID");
        }
        //是否重复
        List<RoutesEntity> routesEntityList = routesService.list();
        for (int i = 0; i < routesEntityList.size(); i++) {
            RoutesEntity routes = routesEntityList.get(i);
            if (Objects.equals(routes.getSubmenuId(), param.getSubmenuId()) && Objects.equals(routes.getMenuId(), param.getMenuId())) {
                return Result.failure("关联已存在");
            }
        }
        //保存||修改
        if (routesEntity.getId() == 0) {
            routesEntity.setId(null);
            if (routesService.save(routesEntity)) {
                return Result.success();
            }
            return Result.failure("保存失败，请稍后重试");
        } else {
            if (routesService.updateById(routesEntity)) {
                return Result.success();
            }
            return Result.failure("修改失败，请稍后重试");
        }
    }

    @Operation(summary = "根据id删除")
    @PostMapping("/delete")
    public Result delete(@RequestBody @Validated IdParam param) {
        //ID是否存在
        if (exist(param) == 0) {
            return Result.failure("关联不存在");
        }
        //删除
        if (routesService.removeById(param.getId())) {
            return Result.success();
        } else {
            return Result.failure("删除失败，请稍后重试");
        }
    }

}
